<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$page_id = MiscUtils::getParam('i', 0);
$inst_id = MiscUtils::getParam('inid', NULL);
$page = MiscUtils::getParam('pg', 1);
$SIZE = 20;
$myPdo = DbUtils::createPdoInst();
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp', NULL, 't.title', 'ASC', $page, $SIZE, NULL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  outline: 0;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
  width: 500px;
}
table.list td.checkall {
  vertical-align: middle;
  text-align: center;
  border-right: 0;
}
table.list td.header {
  vertical-align: middle; 
  height: 25px; 
  border-right: 1px solid #ddd; 
  background-color: #fff;"
}
table.list td.checkall {
  padding-left: 5px;
  border-right: 0;
}
table.list th.nosort {
  text-align: left;
  background-color: #dababa;
  padding: 2px; 
  border-right: 1px solid #fff;
  border-bottom: 1px solid #fff;
  font-weight: bold;
  vertical-align: middle;
}
table.list tr.none td {
  padding: 2px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
}
table.list tr.rowodd td {
  padding: 2px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 2px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
.image_button {
  background: url(../../image/btn/bg.png) repeat;
  border-bottom: 2px solid #666666;
  border-right: 2px solid #666666;
  border-top: 1px solid #bbbbbb;
  border-left: 1px solid #bbbbbb;
  padding: 2px 5px;
  margin-left: 5px;
  font-family: Verdana;
  font-weight: bold;
  cursor: pointer;
  font-size: 12px;
  display: inline-block;
}
.image_button:hover {
  background-color: #ddd;
}
</style>
</head>
<body>
  <table id="lists" class="list" style="" cellPadding=0 cellSpacing=0>
    <tr>
      <td class="header checkall"><input id="items_select" type="checkbox"></input></td>
      <td class="header" colSpan="11">
        <table>
          <tr>
            <td>
              <select id="items_actions">
                <option>Actions...</option>
              </select>
            </td>
            <td>
              <button onclick="parent.location.href='http://<?php echo URL ?>admin.php?i=<?php echo $page_id; ?>&script=index_comp'">Create</button>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <th class="nosort" style="width: 20px;"></th>
      <th class="nosort">Title</th>
      <th class="nosort" style="width: 80px;">Date</span> </th>
      <th class="nosort" width="50px" style="border-right: 1px solid #ddd;"></th>
    </tr>
  </table>
  <div id="pagination"></div>
  <script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
  <script type="text/javascript" src="../../../incls/js/myutils.js"></script>
  <script type="text/javascript" src="../../../incls/js/pagination.js"></script>
  <script type="text/javascript">
    var _load = null;
    var _items = JSON.parse('<?php echo addslashes(json_encode($items)); ?>');
    var _selects = [];
  
    $(function(){
      if (!document.getElementById || !document.createElement || !arguments || arguments.callee._loaded) {
        return;
      }
      arguments.callee._loaded = true;
  
      elm = document.getElementById('pagination');
      new ModulePagination(document, elm, null, null, null, null, {
        page: _items.p,
        total: _items.t,
        limit: _items.s,
        callbackFunc: function(page) {
          parent.location.href = 'http://<?php echo URL . 'admin.php?i=' . $page_id; ?>&pg=' + page;
        },
        contenxt: this
      });
      
      var select, actions;
      select = document.getElementById('items_select');
      select.onclick = function() {
        for (var idx in  _selects) {
          var obj = _selects[idx];
          obj.checked = this.checked;
        }
      };
      
      actions = document.getElementById('items_actions');
      actions.options[actions.options.length] = new Option('Delete');
      actions.onchange = function() {
        var r = window.confirm('ARE YOU SURE?');
        if (r) {
          if (this.selectedIndex == 1) {
            var deletes = [];
            for (var idx in  _selects) {
              var obj = _selects[idx];
              if (obj.checked) {
                deletes[deletes.length] = obj._obj;
              }
            }
            $.ajax({
              type: "POST",
              url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
              data: "action=remove&q=" + encodeURIComponent(JSON.stringify(deletes)),
              success: function(msg){
                try {
                  JSON.parse(msg);
                  select.checked = false;
                  actions.selectedIndex = 0;
                  parent.location.reload();
                } catch (ex) {
                  alert( "Error: " + ex );
                }
              }
            });
          }
        }
      };
      set_items();
    });
    
    function set_items() {
      var elm, table, tr, td;
      table = $("#lists")[0];
      MyUtils.removeTableRows(table, 3);
      for (var idx in _items.d) {
        var obj = _items.d[idx];
        tr = table.insertRow(-1);
        tr.className = (idx % 2 == 0) ? 'roweven' : 'rowodd';
  
        td = tr.insertCell(-1);
        td.style.textAlign = 'center'
        elm = document.createElement('input');
        elm.type = 'checkbox';
        elm._obj = obj;
        _selects.push(elm);
        td.appendChild(elm);

        td = tr.insertCell(-1);
        elm = document.createElement('a');
        elm.href = '#';
        elm._obj = obj;
        elm.onclick = function() {
          parent.location.href = 'http://<?php echo URL ?>admin.php?i=<?php echo $page_id; ?>&script=index_comp&comp_t=' + this._obj.track_id;
          return false;
        }
        elm.appendChild(document.createTextNode(obj.title));
        td.appendChild(elm);
  
        td = tr.insertCell(-1);
        td.appendChild(document.createTextNode(obj.date));
        
        td = tr.insertCell(-1);
        elm = document.createElement('img');
        elm.src = 'images/delete24.png';
        elm.style.cursor = 'pointer';
        elm.style.marginLeft = '5px';
        elm._obj = obj;
        elm.onclick = function() {
          var r = window.confirm('ARE YOU SURE?');
          if (r) {
            $.ajax({
              type: "POST",
              url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
              data: "action=remove&q=" + encodeURIComponent(JSON.stringify([ this._obj ])),
              success: function(msg){
                try {
                  JSON.parse(msg);
                  location.reload();
                } catch (ex) {
                  alert( "Error: " + ex );
                }
              }
            });
          }
        };
        td.appendChild(elm);
      }
  
      if (_items.d.length == 0) {
        tr = table.insertRow(-1);
        tr.className = 'roweven';
        td = tr.insertCell(-1);
        td.colSpan = 9;
        td.appendChild(document.createTextNode('None'));
      }
    };
  </script>
</body>
</html>